(function (G) {
    G.vue({
        data: {
            LIST_count:1,
            LIST_index:0,
            userId:'',
            type:'',
            longitude:0,
            latitude:0,
            keyword:''
        },
        methods: {
            searchName(event) {
                // this.data.LIST_datas[0].keyword = event.detail.value
                this.data.LIST_datas[0].pageNum = 1
                let list = this.LIST_get()
                list[0].get.data.keyword = event.detail.value
                this.LIST_set(list)
                this.LIST_refresh()
            },
            edit(event) {
                let index = event.currentTarget.dataset.index
                var modeleditquery = {
                    values: index == -1 ? {} : this.data.LIST_datas[0].rows[index],
                    title: index == -1 ? '新增资源设施' : '修改资源设施',
                    meth: index == -1 ? 'POST' : 'PUT',
                    refresh: (next,data)=>{//后修改
                        if(data.gridId){//修改资源网格
                            G.get('/api/model/grid',{id:data.gridId}).then(grid => {
                                G.put('/api/model/facility',{
                                    id:data.id,
                                    unitId:grid.unitId,
                                    userId:grid.userId,
                                    enterpriseId:grid.enterpriseId
                                })
                            })
                        }
                        this.LIST_refresh(0)
                    },
                    models: {},
                    url: '/api/model/facility',
                    submit: function (data) {
                        if(data.address){
                            let address = G.string(data.address).parse({})
                            data.address = address.address
                            data.longitude = address.longitude
                            data.latitude = address.latitude
                        }
                        return data;
                    }
                }
                if(modeleditquery.values.address){
                    modeleditquery.values.address = {
                        longitude: modeleditquery.values.longitude,
                        latitude: modeleditquery.values.latitude,
                        address: modeleditquery.values.address
                    }
                }else{
                    modeleditquery.values.address = {
                        longitude: this.data.longitude,
                        latitude: this.data.latitude,
                        address: this.data.address
                    }
                }
                modeleditquery.models = {
                    name:{
                        label:'设备名称',
                        rule: {
                            NotNull: true///不能为空
                        },
                        type:'input'
                    },
                    type:{
                        label:'设施类型',
                        rule:{ NotNull:true },
                        attr:{
                            actions:[
                                {value:'hw_facility_toilet',name:'公厕'},
                                {value:'hw_facility_post',name:'劳动者港湾'},
                                {value:'hw_facility_transit',name:'垃圾中转站'},
                                {value:'hw_facility_collect',name:'垃圾收集点'},
                                {value:'hw_facility_water_pile',name:'水车加水柱'},
                                {value:'hw_facility_advertising',name:'户外广告'},
                                {value:'hw_facility_septic_tank',name:'化粪池'},
                                {value:'hw_facility_outfit',name:'垃圾分类设施'}
                            ]
                        },
                        type:'select'
                    },
                    gridId:{
                        label:'所属网格',
                        rule:{ NotNull:true },
                        attr:{
                            skey: 'name',///显示字段，默认name
                            vkey: 'id',///取值字段，默认id
                            url: '/api/model/grid?status=1',///查询API
                            canClear: true,///是否可清除
                            title: '请选择所属网格'///标题
                        },
                        type:'onlyselect'
                    },
                    address: {
                        label: '地点',
                        rule:{ NotNull:true },
                        attr: {
                            type: 'mapaddress'
                        },
                        type: 'customer'
                    }
                }
                if(this.data.type == 'hw_facility_toilet'){
                    modeleditquery.models.type.attr.actions = [
                        {value:'hw_facility_toilet',name:'公厕'}
                    ]
                    modeleditquery.models.gridId.attr.url += '&resType=hw_facility_toilet' 
                    //零时数据采集的时候需要 新增的公厕属于石桥铺环卫所
                    // modeleditquery.models.gridId = null
                    // if(index == -1) modeleditquery.models.gridId = '1641341364933365760'
                }else if(this.data.type == 'hw_facility_septic_tank'){
                    modeleditquery.models.gridId.attr.url += '&resType=hw_facility_septic_tank' 
                    modeleditquery.models.type.attr.actions = [
                        {value:'hw_facility_septic_tank',name:'化粪池'}
                    ]
                }else{
                    modeleditquery.models.gridId.attr.url += '&resType=other' 
                }
                G.Storage.set('modeleditquery', modeleditquery)
                G.$go('/pages/model/edit/edit')
            },
            go(event){
                let index = event.currentTarget.dataset.index,item = this.data.LIST_datas[0].rows[index]
                let selectFaclity = G.Storage.get('selectFaclity')
                if(selectFaclity){
                    selectFaclity(item)
                    this.$back()
                    return false
                }
                wx.openLocation({
                  latitude: item.latitude,
                  longitude: item.longitude,
                  name: item.name,
                  address: item.address
                })
            }
        },
        mounted(query) {
            let userId = G.Storage.get('userInfo').userId
            wx.setNavigationBarTitle({
              title: query.name + ' 台账',
            })
            wx.getLocation({
                type: 'gcj02',
                success:res=> {
                    this.setData({
                        userId,
                        LIST_index: query.code == 1 ? 0 : 1,
                        type:query.type,
                        latitude:res.latitude,
                        longitude:res.longitude
                    })
                    this.LIST_init([G.list({
                        get:{url:'/v1/facility',data:{type:this.data.type,latitude:this.data.latitude,longitude:this.data.longitude,keyword:''}}
                    })])
                    this.LIST_loads()
                }
            })
            
        }
    })
})(wx.$)